package lvbu.wang.lvbuoperator.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import lvbu.wang.lvbuoperator.R;
import lvbu.wang.lvbuoperator.bean.BluetoothInfoBean;


/**
 * Created by DaiQing.Zha on 2016/11/3 0003.
 */
public class BluetoothListAdapter extends BaseAdapter {

    private Context context;
    private List<BluetoothInfoBean> bluetoothInfoBeanList;

    public BluetoothListAdapter(Context context, List<BluetoothInfoBean> bluetoothInfoBeanList) {
        this.context = context;
        this.bluetoothInfoBeanList = bluetoothInfoBeanList;
    }

    @Override
    public int getCount() {
        return bluetoothInfoBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return bluetoothInfoBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if(convertView==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_bluetooth_list, null);
            holder.tv_name = (TextView)convertView.findViewById(R.id.tv_name);
            holder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);
            holder.tv_rssi = (TextView) convertView.findViewById(R.id.tv_rssi);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        BluetoothInfoBean bluetoothInfoBean = bluetoothInfoBeanList.get(position);
        if ("".equals(bluetoothInfoBean.getName()) || (null == bluetoothInfoBean.getName())){

            holder.tv_name.setText(context.getString(R.string.bluetoothList_unKnowName));
        }else{

            holder.tv_name.setText(bluetoothInfoBean.getName());
        }
        holder.tv_address.setText(bluetoothInfoBean.getAddress());
        holder.tv_rssi.setText("RSSI:" + bluetoothInfoBean.getRssi() + "db");
        return convertView;
    }

    class ViewHolder{

        private TextView tv_name;
        private TextView tv_address;
        private TextView tv_rssi;
    }
}
